package be.android.forap.network;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import be.android.forap.R;

import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.android.AndroidAuthSession;

import com.dropbox.client2.session.TokenPair;


public class DropboxActivity extends Activity {

	private static final String TAG = "DropBoxLoginActivity";

	public static final String AUTH = "be.android.forap.AUTH_DROPBOX";
	public static final String SYNC_CONTACTS = "be.android.forap.SYNC_CONTACTS_DROPBOX";

	private DropboxAPI<AndroidAuthSession> mDBApi;
	
	private Button mLinkButton;
	private ProgressDialog mProgressDialog = null;

	private boolean clicked = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dropbox_link);
		// We create a new AuthSession so that we can use the Dropbox API.
		mDBApi = DropboxUtil.initializeSession();

		mLinkButton = (Button) findViewById(R.id.drop_button_link);
		TextView info = (TextView) findViewById(R.id.drop_textview_info);
		info.setText("Welcome to forAp. \nThis app requires a link with dropbox for contact and key synchronizing between devices. " +
				"\nNote: Dropbox doesn't need to be installed on your android.");
		
	}

	public void onClick(View v){
		switch (v.getId()) {
		case R.id.drop_button_link:
			clicked = true;
			showProgress();
			mDBApi.getSession().startAuthentication(DropboxActivity.this);			
			break;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		AndroidAuthSession session = mDBApi.getSession();

		// The next part must be inserted in the onResume() method of the
		// activity from which session.startAuthentication() was called, so
		// that Dropbox authentication completes properly.
		if (session.authenticationSuccessful()) {
			try {
				Log.i(TAG, "Success authenticating");
				Toast.makeText(this, "U have been succesfully authentificated with dropbox", Toast.LENGTH_LONG).show();
				// Mandatory call to complete the auth
				session.finishAuthentication();

				// Store it locally in our app for later use
				TokenPair tokens = session.getAccessTokenPair();
				DropboxUtil.storeKeys(tokens.key, tokens.secret);
				Intent intent = this.getIntent();
				this.setResult(RESULT_OK, intent);
				finish();
			} catch (IllegalStateException e) {
				Log.e(TAG,"Couldn't authenticate with Dropbox:" + e.getLocalizedMessage());
				Log.i(TAG, "Error authenticating", e);
			}
		}else{
			if(clicked){
				Toast.makeText(this, "Dropbox authentification failed", Toast.LENGTH_LONG).show();
				Log.i(TAG, "CANCELLED");
				clicked = false;
			}
		}
		hideProgress();
	}

	public void onBackPressed() {
		this.setResult(RESULT_CANCELED);
		this.finish();

	}

	private void showProgress(){
		mLinkButton.setEnabled(false);
		mProgressDialog = ProgressDialog.show(this, "Please wait...", "Connecting to dropbox", true, false);
		mProgressDialog.setCancelable(false);
	}

	private void hideProgress() {
		mLinkButton.setEnabled(true);
		if(mProgressDialog != null){
			mProgressDialog.dismiss();
			mProgressDialog = null;
		}

	}
}
